#include "monitorwidget.h"
#include "ui_monitorwidget.h"
#include <QDebug>
#include <winsock2.h>
#include <ws2tcpip.h>

MonitorWidget::MonitorWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MonitorWidget),
    m_vlcPlayer(new VLCRTSP(this))
{
    ui->setupUi(this);
    init();

    setAttribute(Qt::WA_OpaquePaintEvent);  // 禁用背景重绘
    setAttribute(Qt::WA_PaintOnScreen);     // 绕过Qt合成器
    QApplication::setAttribute(Qt::AA_DisableHighDpiScaling); // 禁用DPI缩放

    ui->ErrorResetBtn->installEventFilter(this);
    ui->StartBtn->installEventFilter(this);
    ui->StopBtn->installEventFilter(this);
    ui->PauseBtn->installEventFilter(this);
    ui->ProductEndBtn->installEventFilter(this);
    ui->ProcessResetBtn->installEventFilter(this);

    ui->videoLayout->addWidget(m_vlcPlayer);
    m_vlcPlayer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

MonitorWidget::~MonitorWidget()
{
    delete ui;
}

bool MonitorWidget::eventFilter(QObject *watched, QEvent *event) //过滤器
{
    if(watched == ui->ErrorResetBtn){
        if (event->type() == QEvent::MouseButtonPress){
                emit ErrorResetBtn_push();
            }
            //return true;  //过滤
            return false; //不过滤
    }
    else if(watched == ui->ProcessResetBtn){
        if(event->type() == QEvent::MouseButtonPress){
            emit reset();
            return false;
        }
    }
    else if(watched == ui->ProductEndBtn){
        if(event->type() == QEvent::MouseButtonPress){
            emit ProductEndBtn_push();
            return false;
        }
    }
    else if(watched == ui->StartBtn){
        if(event->type() == QEvent::MouseButtonPress){
            start();
            return false;
        }
    }
    else if(watched == ui->StopBtn){
        if(event->type() == QEvent::MouseButtonPress){
            stop();
            return false;
        }
    }
    else if(watched == ui->PauseBtn){
        if(event->type() == QEvent::MouseButtonPress){
            pause();
            return false;
        }
    }

    else{

        return QWidget::eventFilter(watched,event);
    }
    return true;
}

void MonitorWidget::init()
{
    QString btnName = "一键启动";
    QString btnStyle = "border-image: url(:/new/prefix1/Resource/btn1.png);background-color: rgb(0, 0, 0,0);";
    ui->StartBtn->setPicName(btnStyle,btnName);
    QString styleBtn = "background-color: rgb(145,113,85,%1);";
    ui->StartBtn->setBtnColorStyle(styleBtn);

    btnName = "一键关闭";
    btnStyle = "border-image: url(:/new/prefix1/Resource/btn2.png);background-color: rgb(0, 0, 0,0);";
    ui->StopBtn->setPicName(btnStyle,btnName);
    styleBtn = "background-color: rgb(96,66,47,%1);";
    ui->StopBtn->setBtnColorStyle(styleBtn);

    btnName = "设备暂停";
    btnStyle = "border-image: url(:/new/prefix1/Resource/btn3.png);background-color: rgb(0, 0, 0,0);";
    ui->PauseBtn->setPicName(btnStyle,btnName);
    styleBtn = "background-color: rgb(146,154,59,%1);";
    ui->PauseBtn->setBtnColorStyle(styleBtn);

    btnName = "结束";
    btnStyle = "border-image: url(:/new/prefix1/Resource/btn4.png);background-color: rgb(0, 0, 0,0);";
    ui->ProductEndBtn->setPicName(btnStyle,btnName);
    styleBtn = "background-color: rgb(92,137,171,%1);";
    ui->ProductEndBtn->setBtnColorStyle(styleBtn);

    btnName = "流程复位";
    btnStyle = "border-image: url(:/new/prefix1/Resource/btn5.png);background-color: rgb(0, 0, 0,0);";
    ui->ProcessResetBtn->setPicName(btnStyle,btnName);
    styleBtn = "background-color: rgb(128,83,104,%1);";
    ui->ProcessResetBtn->setBtnColorStyle(styleBtn);

    btnName = "获取Token";
    btnStyle = "border-image: url(:/new/prefix1/Resource/btn8.png);background-color: rgb(0, 0, 0,0);";
    ui->ErrorResetBtn->setPicName(btnStyle,btnName);
    styleBtn = "background-color: rgb(58,27,125,%1);";
    ui->ErrorResetBtn->setBtnColorStyle(styleBtn);
}

void MonitorWidget::start()
{
    QString url = "rtsp://192.168.2.97:8554/live/stream";

    m_vlcPlayer->playRTSP(url);
}

void MonitorWidget::stop()
{
    m_vlcPlayer->closePlayer();
}

void MonitorWidget::pause()
{
    m_vlcPlayer->stop();
}


